home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / varia / egebook.lha / ege.book / 1 / Course2.C < prev    next >
C/C++ Source or Header  |  1992-06-04  |  2KB  |  65 lines

  1. #include <stdio.h>
  2.  
  3. class Teacher {
  4.   char *name;               // name of teacher
  5. public:
  6.   void setName(char *newName){   // member function
  7.     name = newName;
  8.   };
  9.   void print(){
  10.     printf("the teachers name is: %s \n", name);
  11.   };
  12. };
  13.  
  14. class Student{
  15.   char *name;       // name of student
  16. };
  17.  
  18. class Course {
  19.   char *number;          
  20.   char *time;
  21.   Student *students;               // NEW: variable number of 
  22.                                    //      students allowed
  23. public:
  24.   Teacher *teacher;
  25.   Course(){                        // constructor
  26.     number = "unassigned";         //     with no parameters
  27.     time = "TBA";
  28.     students = new Student[25];    // create 25 students
  29.   };
  30.   Course(char *n, char *t, int s = 25){ // constructor
  31.     number = n;                    //      with parameters
  32.     time = t;
  33.     students = new Student[s];     // create a specified
  34.   };                               //     number of students
  35.   ~Course(){
  36.     delete students;               // free all students
  37.   };
  38.   void print(){
  39.     printf("Course number: %s, at: %s \n", number, time);
  40.   };
  41. };
  42.  
  43. main() {
  44.   Course c1;                 // constructor is called 
  45.                              // with no parameters
  46.   Course c2("COP 4225","MW 1030-1200");
  47.                              // constructor is called
  48.                              // with two explicit parameters
  49.   c1.print();
  50.   c2.print();
  51.  
  52.   Course *c3;                // C++ allows to mix
  53.   Course *c4;                //  declarations and statements
  54.  
  55.   c3 = new Course;           // constructor is called
  56.                              // with no parameters
  57.   c4 = new Course("COP 6611","TR 1030-1200");
  58.                              // constructor is called
  59.                              // with two explicit parameters
  60.   c3->print();
  61.   c4->print();
  62. }
  63.  
  64.  
  65.